package org.mapdroid.maps.gmaps.overlay.info;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.mapdroid.R;
import org.mapdroid.data.adt.MdService;
import org.mapdroid.maps.gmaps.MdMapActivity;
import org.mapdroid.maps.gmaps.MdMapView;
import org.mapdroid.maps.gmaps.overlay.MdMarkerItemizedOverlay;
import org.mapdroid.utils.MdDirectionsActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ServiceInfoActivity extends Activity implements IMarkerInfo{
	private ListView listView;
	private ImageView imageView;
	private List<Integer> imageGallery;
	private TextView titleView;
	private boolean hasPhone, hasWebSite/*, hasGallery*/;
	private String phone, webSite;
	private MdService service;
	
	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marker_info_layout);
        
        listView 	= (ListView) findViewById(R.id.info_list);
        listView.setOnItemClickListener(this);
        imageView 	= (ImageView) findViewById(R.id.info_image);
        titleView 	= (TextView) findViewById(R.id.info_name); 
        
        hasPhone	= false;
        hasWebSite	= false;
        //hasGallery 	= false;
        
        phone		= null;
        webSite		= null;
        
        Intent markerIntent 	= super.getIntent();
        service = markerIntent.getParcelableExtra(MdMarkerItemizedOverlay.OBJECT);
        
        initDialog(service, R.layout.marker_single_voice_layout);
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy();
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    }

    @Override
    public void onResume(){
    	super.onResume();
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.service_info_menu, menu);
	    if(super.getCallingActivity().getShortClassName().contains("MdMapActivity")){
	    	menu.getItem(0).setTitle(R.string.search_parking_around_item);
	    	menu.getItem(0).setTitleCondensed(getString(R.string.search_parking_around_item_condensed));
	    }
	    menu.getItem(3).setEnabled(hasPhone);
	    menu.getItem(4).setEnabled(hasWebSite);
	    //menu.getItem(5).setEnabled(hasGallery);
	    return true;
	}
    
    @Override
	public void initDialog(Object o, int singleRowLayoutResId){
    	MdService service = (MdService) o;
    	//Title
    	String serviceName = service.getName();
		if(serviceName!=null)
			titleView.setText(serviceName);
		else
			titleView.setText(getString(R.string.service_info_title));
		
		//Photo
		int photoId = service.getPhoto();
		if(photoId>0)
			this.imageView.setImageResource(photoId);
		
		//SimpleAdapter
 		List<Map<String, Object>> 	adapterList = new ArrayList<Map<String, Object>>();
 		Map<String, Object> 		tempMap = null;
		
		String[] tmpStringVector = new String[3];
		tmpStringVector[0]="Label";
		tmpStringVector[1]="Value";
		tmpStringVector[2]="Image";
		
		int[] tmpId = new int[3];
		tmpId[0] = org.mapdroid.R.id.marker_voice_label;
		tmpId[1] = org.mapdroid.R.id.marker_voice_text;
		tmpId[2] = org.mapdroid.R.id.marker_icon;
		
		//Category
		int tmpInt = service.getCategory();
		if(tmpInt>-1){
			tempMap = new Hashtable<String, Object>();
			tempMap.put("Label", getString(R.string.category_label));
			tempMap.put("Value", getString(MdService.CATEGORIES_RESOURCES[tmpInt]));
			tempMap.put("Image", android.R.drawable.ic_menu_agenda);
			adapterList.add(tempMap);
		}
		
		//Address
		String tmpString = service.getAddress();
		if(tmpString!=null){
			tempMap = new Hashtable<String, Object>();
			tempMap.put("Label", getString(R.string.address_label));
			tempMap.put("Value", tmpString);
			tempMap.put("Image", android.R.drawable.ic_menu_directions);
			adapterList.add(tempMap);
		}
		
		//Phone
		tmpString = service.getPhoneNumber();
		if(tmpString!=null){
			hasPhone = true;
			phone=tmpString;
			tempMap = new Hashtable<String, Object>();
			tempMap.put("Label", getString(R.string.phone_label));
			tempMap.put("Value", tmpString);
			tempMap.put("Image", android.R.drawable.ic_menu_call);
			adapterList.add(tempMap);
		}
		
		//Description
		tmpString = service.getDescription();
		if(tmpString!=null){
			tempMap = new Hashtable<String, Object>();
			tempMap.put("Label", getString(R.string.description_label));
			tempMap.put("Value", tmpString);
			tempMap.put("Image", android.R.drawable.ic_menu_info_details);
			adapterList.add(tempMap);
		}

		//Photo Gallery
		this.imageGallery = service.getGallery();
		if(imageGallery!=null){
			//hasGallery=true;
			tempMap = new Hashtable<String, Object>();
			tempMap.put("Label", getString(R.string.photo_label));
			tempMap.put("Value", this.getString(R.string.gallery_list_item));
			tempMap.put("Image", android.R.drawable.ic_menu_gallery);
			adapterList.add(tempMap);
		}
					
		//Website
		tmpString = service.getLink();
		if(tmpString!=null){
			hasWebSite = true;
			webSite = tmpString;
			tempMap = new Hashtable<String, Object>();
			tempMap.put("Label", getString(R.string.website_label));
			tempMap.put("Value", tmpString);
			tempMap.put("Image", android.R.drawable.ic_menu_mapmode);
			adapterList.add(tempMap);
		}
		
		SimpleAdapter adapter = new SimpleAdapter(this, adapterList, singleRowLayoutResId, tmpStringVector, tmpId);
		listView.setAdapter(adapter);
    }
    
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		TextView textView = (TextView) arg1.findViewById(R.id.marker_voice_label);
		String label = (String) textView.getText();
		
		if(label.contentEquals(getString(R.string.phone_label))){	
			textView = (TextView) arg1.findViewById(R.id.marker_voice_text);
			String value = (String) textView.getText();
			
			call(value);
		}
		if(label.contentEquals(getString(R.string.website_label))){	
			textView = (TextView) arg1.findViewById(R.id.marker_voice_text);
			String value = (String) textView.getText();
			
			goToWebSite(value);
		}
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();

		switch(itemId){
		//Product / Service
			case R.id.search_around_item:
				Intent data = new Intent();
				data.putExtra(MdMarkerItemizedOverlay.RANGE, true);
				data.putExtra(MdMarkerItemizedOverlay.POSITION, MdMapView.fromGeoPointToLocation(service.getPosition()));
				
				this.setResult(Activity.RESULT_OK, data);
				this.finish();
				return true;

		//Add to My Contact-List
			case R.id.add_to_my_contacts_item:
				ContentValues values = new ContentValues();
				values.put(android.provider.Contacts.People.NAME, service.getName());
				//values.put(android.provider.Contacts.People.NUMBER, service.getPhoneNumber());
				//values.put(android.provider.Contacts.People.STARRED, 0);
	
				Uri uri = getContentResolver().insert(android.provider.Contacts.People.CONTENT_URI, values);
				if(uri!=null)
					Toast.makeText(this, R.string.contact_added_text, Toast.LENGTH_SHORT).show();
				return true;
			
		//StreetView
			case R.id.street_view_item:
				Intent streetViewIntent = new Intent(Intent.ACTION_VIEW);
				
				streetViewIntent.setData(Uri.parse("google.streetview:cbll=" + 
													service.getPosition().getLatitudeE6()/1E6 + 
													"," + 
													service.getPosition().getLongitudeE6()/1E6 + 
													"&cbp=1,0,,0,1.0&mz=1"));
				this.startActivity(streetViewIntent);
				return true;
					
		//Call
			case R.id.call_item:
				call(phone);
				return true;
				
		//WebSite
			case R.id.go_to_website_item:
				goToWebSite(webSite);
				return true;
				
		//Gallery
		/*	if(itemTitle.contentEquals(getString(R.string.view_gallery_item))){
			}*/
			
		//Directions
			case R.id.directions_item:
				Location loc = new Location(LocationManager.GPS_PROVIDER);
				Intent directionsIntent = new Intent(this, MdDirectionsActivity.class);
				double lat = service.getPosition().getLatitudeE6()/1E6;
				double lon = service.getPosition().getLongitudeE6()/1E6;
				loc.setLatitude(lat);
				loc.setLongitude(lon);
				directionsIntent.putExtra(MdDirectionsActivity.START_POINT, loc);
				directionsIntent.putExtra(MdDirectionsActivity.START_TEXT, service.getName());
				startActivityForResult(directionsIntent, MdMapActivity.SERVICE_INTENT_ID);
				return true;
		}
		return false;
	}
	
	protected void call(String number){
		Intent call = new Intent(Intent.ACTION_DIAL);
		call.setData(Uri.parse("tel:" + number));
		this.startActivity(call);
	}
	
	protected void goToWebSite(String url){
		Intent surf = new Intent(Intent.ACTION_VIEW);
		if(url.contains("http"))
			surf.setData(Uri.parse(url));
		else
			surf.setData(Uri.parse("http://" + url));
		this.startActivity(surf);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		
		switch(requestCode){
			case MdMapActivity.SERVICE_INTENT_ID:
				if(resultCode == Activity.RESULT_OK){
					setResult(Activity.RESULT_OK, data);
					finish();
				}
			break;
		}
	}
}